package com.xjzd.spring.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

public class EnhancerDemo {

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(UserService.class);
        enhancer.setCallback((MethodInterceptor) (obj, method, args1, proxy) -> {
            System.out.println("CGLIB 拦截前");
            Object result = proxy.invokeSuper(obj, args1); // 调用原始方法
            System.out.println("CGLIB 拦截后");
            return result;
        });

        UserService proxy = (UserService) enhancer.create();
        proxy.saveUser();
    }
}
